package com.lb.myeam.core.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.lb.myeam.common.utils.DBUtil;
import com.lb.myeam.core.dao.DepDao;
import com.lb.myeam.core.entity.DepEntity;

public class DepDaoImpl implements DepDao {

	public boolean addDep(DepEntity dep) {
		Connection conn = DBUtil.getConn();
		Statement stmt = null;
		if (null != conn) {
			try {
				stmt = conn.createStatement();
				String sql = "insert into dep values(\'" + dep.getDepId()
						+ "\',\'" + dep.getDempName() + "\',\'"
						+ dep.getLeader() + "\')";
				int num = stmt.executeUpdate(sql);
				return num > 0;
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				DBUtil.close(null, stmt, conn);
			}
		}
		return false;
	}

	public boolean deleteDep(String id) {
		Connection conn = DBUtil.getConn();
		Statement stmt = null;
		if (null != conn) {
			try {
				stmt = conn.createStatement();
				String sql = "delete from dep where dep_id = " + id;
				int num = stmt.executeUpdate(sql);
				return num > 0;
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				DBUtil.close(null, stmt, conn);
			}
		}
		return false;
	}

	public List<DepEntity> query(String name) {
		Connection conn = DBUtil.getConn();
		Statement stmt = null;
		List<DepEntity> list = new ArrayList<DepEntity>();
		if (null != conn) {
			try {
				stmt = conn.createStatement();
				String sql = "select * from dep";
				ResultSet rs = stmt.executeQuery(sql);
				while (rs.next()) {
					list.add(new DepEntity(rs.getString(1), rs.getString(2), rs
							.getString(3)));
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				DBUtil.close(null, stmt, conn);
			}
		}
		return list;
	}

	public boolean updateDep(DepEntity dep) {
		// TODO Auto-generated method stub
		return false;
	}

}
